package com.atguigu.srb.service.sms.config;


import com.atguigu.srb.base.consts.SrbMqConst;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {

    //给业务队列配置死信队列
    //死信交换机、队列 绑定
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange(SrbMqConst.SMS_DEAD_EXCHANGE)
                    .ignoreDeclarationExceptions()
                    .build();
    }
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(SrbMqConst.SMS_DEAD_QUEUE)
                    .build();
    }
    @Bean
    public Binding deadBinding(Exchange deadExchange, Queue deadQueue){
        return BindingBuilder.bind(deadQueue).to(deadExchange)
                .with(SrbMqConst.SMS_DEAD_ROUTING_KEY)
                .noargs();
    }

    //业务交换机 业务队列  绑定
    @Bean
    public Exchange smsExchange(){
        return ExchangeBuilder.topicExchange(SrbMqConst.SMS_EXCHANGE)
                .ignoreDeclarationExceptions()
                .build();
    }
    @Bean
    public Queue smsQueue(){
        return QueueBuilder.durable(SrbMqConst.SMS_QUEUE)
                //绑定死信队列： SMS_QUEUE队列中的消息丢弃或者超时 会自动交给绑定的交换机使用绑定的路由key路由
                .deadLetterExchange(SrbMqConst.SMS_DEAD_EXCHANGE)
                .deadLetterRoutingKey(SrbMqConst.SMS_DEAD_ROUTING_KEY)
                .build();
    }
    @Bean
    public Binding smsBinding(Exchange smsExchange,Queue smsQueue){
        return BindingBuilder.bind(smsQueue).to(smsExchange)
                .with(SrbMqConst.SMS_ROUTING_KEY)
                .noargs();
    }

    @Bean
    public MessageConverter messageConverter(){
        //json字符串转换器
        return new Jackson2JsonMessageConverter();
    }

}